﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QRCode.Models;
using System.Net;

namespace QRCode.Controllers
{
    public class SalesPromotionController : Controller
    {
        public SalesPromotionDBContext db = new SalesPromotionDBContext();

        //
        // GET: /SalesPromotion/

        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            return View(db.SalesPromotions.ToList());
        }

        //
        // GET: /SalesPromotion/Details/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Details(int id = 0)
        {
            SalesPromotion salespromotion = db.SalesPromotions.Find(id);
            if (salespromotion == null)
            {
                return HttpNotFound();
            }
            return View(salespromotion);
        }

        //
        // GET: /SalesPromotion/Create

        [Authorize(Roles = "Administrator")]
        public ActionResult Create()
        {
            List<SelectListItem> co = new List<SelectListItem>();
            List<Location> locations = new List<Location>();
            SelectListItem li = new SelectListItem();

            QRCode.Controllers.LocationController cc = new LocationController();
            locations = cc.db.Locations.ToList();

            foreach (Location item in locations)
            {
                li.Text = item.Name;
                li.Value = item.ID.ToString();
                co.Add(li);
            }

            List<SelectListItem> cp = new List<SelectListItem>();
            List<PromotionType> promotionTypes = new List<PromotionType>();
            SelectListItem lp = new SelectListItem();

            QRCode.Controllers.PromotionTypeController cn = new PromotionTypeController();
            promotionTypes = cn.db.PromotionTypes.ToList();

            foreach (PromotionType item in promotionTypes)
            {
                lp.Text = item.Name;
                lp.Value = item.ID.ToString();
                cp.Add(lp);
            }

            ViewBag.ddLocations = co;
            ViewBag.ddPromotionTypes = cp;

            return View();
        }

        //
        // POST: /SalesPromotion/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Administrator")]
        public ActionResult Create(SalesPromotion salespromotion)
        {
            if (ModelState.IsValid)
            {

                string LocationID = Request.Form.GetValues("ddLocations")[0];
                string PromotionTypeID = Request.Form.GetValues("ddPromotionTypes")[0];

                salespromotion.LocationID = Convert.ToInt32(LocationID);
                salespromotion.PromotionTypeID = Convert.ToInt32(PromotionTypeID);

                Guid guid = Guid.NewGuid();

                string googleChartUrl = "http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=http://www.qrcode-coupons.com/Coupon/Create/?qr=" + guid.ToString();

                string imageLocation = Server.MapPath("~/Images/QR/") + guid.ToString() + ".jpg";

                WebClient webClient = new WebClient();
                webClient.DownloadFile(googleChartUrl, imageLocation);

                salespromotion.Code = guid.ToString();
                salespromotion.Image = imageLocation;

                db.SalesPromotions.Add(salespromotion);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(salespromotion);
        }

        //
        // GET: /SalesPromotion/Edit/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id = 0)
        {
            SalesPromotion salespromotion = db.SalesPromotions.Find(id);
            if (salespromotion == null)
            {
                return HttpNotFound();
            }
            return View(salespromotion);
        }

        //
        // POST: /SalesPromotion/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(SalesPromotion salespromotion)
        {
            if (ModelState.IsValid)
            {
                db.Entry(salespromotion).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(salespromotion);
        }

        //
        // GET: /SalesPromotion/Delete/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Delete(int id = 0)
        {
            SalesPromotion salespromotion = db.SalesPromotions.Find(id);
            if (salespromotion == null)
            {
                return HttpNotFound();
            }
            return View(salespromotion);
        }

        //
        // POST: /SalesPromotion/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Administrator")]
        public ActionResult DeleteConfirmed(int id)
        {
            SalesPromotion salespromotion = db.SalesPromotions.Find(id);
            db.SalesPromotions.Remove(salespromotion);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}